﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.IO;
using MM.Common.Base;
using MM.Common.DBBase;

namespace MM.Common.Information
{
    public class PDFFactory
    {
        public string FileName = "";

        // Methods
        public bool CreatePdf(string filecode)
        {
            bool flag = true;
            object obj2 = new object();
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("filecode", filecode) });
            SystemInfo.DbData.ExecuteScalar("SELECT FILE_CONTENT FROM MXHLY_SYS_FILE WHERE FILE_CODE = :filecode", parms, ref obj2);
            string objdata = Gzip.GzipDeCompress((byte[])obj2);
            try
            {
                if (obj2 != null)
                {
                    string filename = SystemInfo.RunPath + @"\Temp\" + filecode + ".PDF";
                    flag = this.WritePdf(SystemInfo.RunPath + @"\Temp", filename, objdata);
                }
            }
            catch (Exception exception)
            {
                flag = false;
                XtraMessageBox.Show("打开文件不成功！\r\n" + exception.Message);
            }
            return flag;
        }

        public bool GetPdf(string filename, ref string obj)
        {
            bool flag = false;
            try
            {
                byte[] inArray = File.ReadAllBytes(filename);
                obj = Convert.ToBase64String(inArray);
                flag = true;
                this.FileName = filename;
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show("读取文件不成功！\r\n" + exception.Message);
            }
            return flag;
        }

        public bool SavePdf(string filecode)
        {
            if (this.FileName == "")
            {
                return true;
            }
            bool flag = true;
            try
            {
                byte[] paramValue = Gzip.GzipCompress(Convert.ToBase64String(File.ReadAllBytes(this.FileName)));
                string commandtext = "UPDATE MXHLY_SYS_FILE SET FILE_CONTENT = :content WHERE FILE_CODE = :code";
                DataParamCol col2 = new DataParamCol();
                col2.Add(new InputDataParam("content", paramValue, new object()));
                col2.Add(new InputDataParam("code", filecode));
                DataParamCol parms = col2;
                if (SystemInfo.DbData.ExecuteSql(commandtext, parms, 1) != 1)
                {
                    flag = false;
                }
            }
            catch (Exception exception)
            {
                SystemInfo.DbData.TransactionRollback();
                flag = false;
                XtraMessageBox.Show(exception.Message);
            }
            return flag;
        }

        protected bool WritePdf(string path, string filename, string objdata)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            if (!info.Exists)
            {
                info.Create();
            }
            bool flag = false;
            try
            {
                byte[] buffer = Convert.FromBase64String(objdata);
                FileStream stream = File.Open(filename, FileMode.Create);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                flag = true;
                this.FileName = filename;
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show("创建文件不成功！\r\n" + exception.Message);
            }
            return flag;
        }
    }
}
